﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace msBsc.SvRef
{
    public class TaobaoIpGet
    {
        public uint IpToInt(string ip)
        {
            byte[] bytes = IPAddress.Parse(ip).GetAddressBytes();
            return (uint)bytes[3] + (((uint)bytes[2]) << 8) + (((uint)bytes[1]) << 16) + (((uint)bytes[0]) << 24);
        }
        public void getIp()
        {
            SRWebClient oSRWebClient = new SRWebClient();
            string strPrevCity = "";
            uint iStartIp = 0;
            uint iPrevIp = 0;
            for (int a = 58; a < 255; a++)
            {

                for (int b = 14; b < 255; b++)
                {

                    for (int c = 0; c < 255; c++)
                    {

                        for (int d = 0; d < 255; d++)
                        {
                            string strip = a.ToString() + "." + b.ToString() + "." + c.ToString() + "." + d.ToString();

                            string strJson = oSRWebClient.Request("http://ip.taobao.com/service/getIpInfo.php?ip=" + strip, new RequestData(), false);
                            System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();

                            Dictionary<string, object> JsonData = (Dictionary<string, object>)s.DeserializeObject(strJson);
                            if (JsonData != null && JsonData["code"] != null && JsonData["code"].ToString() == "0" && JsonData["data"] != null)
                            {
                                Dictionary<string, object> ipdata = (Dictionary<string, object>)(JsonData["data"]);
                                if (ipdata["country_id"].ToString() != "CN")
                                {
                                    continue;
                                }
                                if (iStartIp == 0)
                                {
                                    iStartIp = IpToInt(strip);
                                }
                                if (iPrevIp == 0)
                                {
                                    iPrevIp = IpToInt(strip);
                                }
                                if (strPrevCity == "")
                                {
                                    strPrevCity = ipdata["region_id"].ToString();
                                }

                                if (strPrevCity != ipdata["region_id"].ToString())
                                {
                                    AddRow(iStartIp, iPrevIp, strPrevCity);
                                    iStartIp = IpToInt(strip);
                                    iPrevIp = IpToInt(strip);
                                    strPrevCity = ipdata["region_id"].ToString();
                                }
                                else
                                {
                                    iPrevIp = IpToInt(strip);
                                }





                                //ipdata["region_id"]
                            }
                        }
                    }
                }
            }
        }
        private int irownum=0;
        public void AddRow(uint StartIP, uint EndIP, string IP_ProvinceId)
        {
            using(Epm.AppLogic.Framework.CDbAccess oCDbAccess=new Epm.AppLogic.Framework.CDbAccess())
            {
                oCDbAccess.ExecuteBySQL("INSERT BSE_PROVINCE_IP SELECT IP_ID=" + irownum.ToString() + ",START_IP=" + StartIP.ToString() + ",END_IP=" + EndIP.ToString() + ",PROVINCE_ID='" + IP_ProvinceId + "'");
            }
            irownum++;
        }

    }
}
